<#include "/common/easyui-css.html">
<#include "/common/easyui-js.html">
<script type="text/javascript" src="/lib/layer/2.4/layer.js"></script>

<div class="easyui-panel" style="padding:5px">
    <ul id="tt" class="easyui-tree" ></ul>
</div>

<div style="margin:5px 0;">
    <a href="#" class="easyui-linkbutton" onclick="getChecked()">分配权限</a>
</div>
<script type="text/javascript">

    $(document).ready(function(){
        $("#tt").tree({
            url:'/role-assign-json/${mRights}/${bRights}',
            method:'get',
            animate:true,
            checkbox: true,
            // 必须是false否则在加载已经配置的子节点下没选中的也会选中
            cascadeCheck: false,
            onCheck: function (node, checked) {
                // 选中时  一般不进行操作 让用户自己选中
                if (checked) {
                    var parentNode = $("#tt").tree('getParent', node.target);
                    if (parentNode != null) {
                        $("#tt").tree('check', parentNode.target);
                    }
                }
                // 取消时一般进行全部取消
                else {
                    var childNode = $("#tt").tree('getChildren', node.target);
                    if (childNode.length > 0) {
                        for (var i = 0; i < childNode.length; i++) {
                            $("#tt").tree('uncheck', childNode[i].target);
                        }
                    }
                }
            }
        });
    });
    function getChecked(){
        var nodes = $('#tt').tree('getChecked');
        var s = '';
        for(var i=0; i<nodes.length; i++){
            if (s != '') s += ',';
            s += nodes[i].id;
        }
        $.ajax({
            url: '/role-assign/${id}',
            async : false,
            data :{ids:s},
            beforeSend: function(){
                layer.msg('分配中', {icon: 2});
            },
            success:function (data) {
                window.parent.$('#role_list_table').datagrid('reload');

            },
            complete: function(){
                window.parent.layer.closeAll();
            }
        })
    }
</script>
